package com.pdsu.xpf.graduation01.control;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.domain.Commodity;
import com.pdsu.xpf.graduation01.domain.CommodityType;
import com.pdsu.xpf.graduation01.domain.Type;
import com.pdsu.xpf.graduation01.service.impl.CommodityServiceImpl;
import com.pdsu.xpf.graduation01.service.impl.CommodityTypeServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.Result;
import com.pdsu.xpf.graduation01.vo.CommodityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/commodity_b")
public class BackCommodityControl {


    final
    CommodityServiceImpl commodityService;
    final
    CommodityTypeServiceImpl commodityTypeService;
    public BackCommodityControl(CommodityServiceImpl commodityService, CommodityTypeServiceImpl commodityTypeService) {
        this.commodityService = commodityService;
        this.commodityTypeService = commodityTypeService;
    }

    @ResponseBody
    @RequestMapping("/findAllCommodity")
    public Result findAllCommodity() throws JsonProcessingException {
       List<CommodityVO> commodities= commodityService.findAllCommodityVO();
       for (CommodityVO commodity:commodities){
           String str = commodity.getImage();
           if (str.startsWith("http://10.0.2.2")){
               String img= "http://127.0.0.1"+str.substring(15);
               commodity.setImage(img);
           }
       }
        return Result.success(commodities);
    }
    @ResponseBody
    @RequestMapping("/findAllCategories")
    public Result findAllCategories() throws JsonProcessingException {
        List<Type> types = commodityTypeService.getTypeList();
        return Result.success(types);
    }
    //getCommodityTypesByTID
    @ResponseBody
    @RequestMapping("/getCommodityTypesByTID")
    public Result getCommodityTypesByTID(int tid) throws JsonProcessingException {
        List<CommodityType> commodityTypes = commodityTypeService.getCommodityListByTID(tid);
        return Result.success(commodityTypes);
    }
    //delCommodityByCID
    @ResponseBody
    @RequestMapping("/delCommodityByCID")
    public Result delCommodityByCID(int cid) throws JsonProcessingException {
       Integer integer= commodityService.delCommodityByCID(cid);
        return Result.success();
    }
    //findAllCheckCommodityVO
    @ResponseBody
    @RequestMapping("/findAllCheckCommodityVO")
    public Result findAllCheckCommodityVO() throws JsonProcessingException {
       List<CommodityVO> commodityVOS = commodityService.findAllCheckCommodityVO();
        for (CommodityVO commodity:commodityVOS){
            String str = commodity.getImage();
            if (str.startsWith("http://10.0.2.2")){
                String img= "http://127.0.0.1"+str.substring(15);
                commodity.setImage(img);
            }
        }
        return Result.success(commodityVOS);
    }

    //upCommodityByCID
    @ResponseBody
    @RequestMapping("/upCommodityByCID")
    public Result upCommodityByCID(int cid) throws JsonProcessingException {
        Integer num = commodityService.upCommodityByCID(cid);
        return Result.success();
    }

    //searchCommodityByKey
    @ResponseBody
    @RequestMapping("/searchCommodityByKey")
    public Result searchCommodityByKey(String key) throws JsonProcessingException {
        List<CommodityVO> commodities = commodityService.searchCommodityByKey("%"+key+"%");
        return Result.success(commodities);
    }

    //updateCommodityByCID
    @ResponseBody
    @RequestMapping("/updateCommodityByCID")
    public Result updateCommodityByCID(int cid,double price,String typename) throws JsonProcessingException {
        System.out.println(cid+"---"+price+typename);
        int ctid = commodityService.getCTIDbyTYPENAME(typename);
        System.out.println(ctid+"--ctid");
        commodityService.updateCommodityByCID(cid,price,ctid);
        return Result.success();
    }
}
